package cn.demomaster.quickpicturer.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtil {
    private static final int REQUEST_MULTIPLE_PERMISSIONS = 1;
    public static boolean isGranted(String permission){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        return false;
    }
    static PermissionResultCallback permissionResultCallback;
    public static void requestPermissions(Activity mContext, String[] permission, PermissionResultCallback permissionResultCallback1) {
        if(permission==null||permission.length == 0){
            permissionResultCallback1.onPermissionsGranted(permission);
            return;
        }
        permissionResultCallback = permissionResultCallback1;
        boolean allGranted = true;
        for(String s : permission){
            int result = ContextCompat.checkSelfPermission(mContext, s);//Returns:PackageManager.PERMISSION_GRANTED if you have the permission, or PackageManager.PERMISSION_DENIED if not.

            if(s.equals(Manifest.permission.READ_MEDIA_IMAGES)){
                if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.S&&Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU){
                    result = PackageManager.PERMISSION_GRANTED;
                }
            }

            if(result== PackageManager.PERMISSION_DENIED){
                allGranted = false;
            }
        }
        if(allGranted){
            permissionResultCallback.onPermissionsGranted(permission);
        }else {
            //permissionResultCallback.onPermissionsDenied();
            if(ActivityCompat.shouldShowRequestPermissionRationale(mContext, permission[0])){
                Toast.makeText(mContext,"请授权后操作",Toast.LENGTH_SHORT).show();
                //permissionResultCallback.onPermissionsDenied();
            }else {
                Toast.makeText(mContext,"申请",Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(mContext, permission, REQUEST_MULTIPLE_PERMISSIONS);
            }
        }
    }

    public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_MULTIPLE_PERMISSIONS) {
            boolean allGranted = true;
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            if (allGranted) {
                // All permissions were granted.
                //performAction();
                permissionResultCallback.onPermissionsGranted(permissions);
            } else {
                // Some or all permissions were denied.
                //showExplanationAndRequestAgainIfNecessary();
                permissionResultCallback.onPermissionsDenied();
            }
            permissionResultCallback = null;
        }
    }

    public static interface PermissionResultCallback {
        void onPermissionsGranted(String[] var1);

        void onPermissionsDenied();
    }
}
